/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Carlos
 */
public class EnvioMensaje extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        /*SocialFP red= new SocialFP();
        try {
                    red.recuperaUjaenSocial();
                }catch (Exception e){
                 notificarError (request, response, "Error", "No se puede recuperar el estado de la red");
                }*/
        
        SocialFP red=  (SocialFP) request.getSession ().getAttribute ("red");
        if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
        String email=  (String) request.getSession ().getAttribute ("email");
         String clave=  (String) request.getSession ().getAttribute ("clave");
        Usuario u = red.loginUsuario(email, clave);
         if (u == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
         String email2= request.getParameter ("Amigo");
         String mensaje=request.getParameter("mensaje");
         if (mensaje=="" || mensaje==null){
             notificarError (request, response, "Error", "No se envió ningun mensaje");
          // RequestDispatcher dispatcher = request.getRequestDispatcher(response.encodeURL("mostrarUsuario"));
           //dispatcher.forward (request, response);
           return;
         }
         if (email2=="" || email2==null){
             notificarError (request, response, "Error", "No seleccionó ningún amigo");
          // RequestDispatcher dispatcher = request.getRequestDispatcher(response.encodeURL("mostrarUsuario"));
           //dispatcher.forward (request, response);
           return;
         }
         Usuario temp = red.buscarUsuarioEmail(email2);
         if ("nadie".equals(email2)){
           u.mensajeMuro(mensaje);
           notificarExito (request, response, "Realizado", "Has enviado el mensaje al muro");
           //RequestDispatcher dispatcher = request.getRequestDispatcher(response.encodeURL("mostrarUsuario"));
           //dispatcher.forward (request, response);
           return;
         }else{
             try{
                 temp.mensajeAmigo(mensaje, u);
                 notificarExito (request, response, "Realizado", "Has enviado el mensaje privado");
                 return;
             }catch(Exception E){
                 notificarError (request, response, "Error", "No se pudo enviar el privado, ¿es tu amigo?");
                  return;
             }
             //RequestDispatcher dispatcher = request.getRequestDispatcher(response.encodeURL("mostrarUsuario"));
             //dispatcher.forward (request, response);
         }
         
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    protected void notificarError (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }   
     protected void notificarExito (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("realizado.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
